<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>编辑分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="__LAYUI__/css/layui.css" rel="stylesheet">
    <style>
        .container {
            display: flex;
            justify-content: start;
        }

        .image-box {
            width: 100px;
            height: 100px;
            border: 2px dashed #ccc;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2em;
            color: #ccc;
            cursor: pointer;
            position: relative;
            margin-right: 20px;
        }

        .image-box img {
            max-width: 100%;
            max-height: 100%;
        }

        .image-box .delete-btn {
            position: absolute;
            top: 0;
            right: 0;
            background-color: red;
            color: white;
            border: none;
            padding: 2px;
            cursor: pointer;
            display: none;
        }

        .user-card {
            padding: 10px;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
        }

        .user-info {
            margin-left: 15px;
            color: #666;
        }

        .vip-tag {
            background: #FFD700;
            color: #333;
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 12px;
            margin-left: 10px;
        }

        .layui-badge-rim {
            margin-left: 8px;
            border: 1px solid #2F9688;
            color: #2F9688;
        }

        /* 分页样式 - 横向布局优化 */
        #pagination {
            text-align: center;
            margin: 20px 0;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: nowrap;
        }

        #pagination a,
        #pagination span,
        li {
            display: inline-flex;
            justify-content: center;
            align-items: center;
            margin: 0 3px;
            padding: 0;
            min-width: 32px;
            height: 32px;
            line-height: 30px;
            border-radius: 2px;
            background-color: #fff;
            color: #666;
            font-size: 13px;
            text-decoration: none;
            transition: all 0.2s;
        }


        #pagination .disabled {
            color: #d2d2d2;
            cursor: not-allowed;
        }

        /* 首页和尾页按钮样式 */
        #pagination .first,
        #pagination .last {
            padding: 0 10px;
        }

        /* 上一页和下一页按钮样式 */
        #pagination .prev,
        #pagination .next {
            padding: 0 10px;
        }

        /* 用户列表样式优化 */
        #userList {
            margin-top: 10px;
            border: 1px solid #eee;
            border-radius: 4px;
        }

        .user-card {
            margin-bottom: 0;
            padding: 12px 15px;
            transition: background-color 0.2s ease;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
        }

        .user-card:last-child {
            border-bottom: none;
        }

        .user-card:hover {
            background-color: #f9f9f9;
        }

        .user-details {
            display: flex;
            flex-direction: column;
            flex: 1;
            margin-left: 10px;
        }

        .user-main {
            display: flex;
            align-items: center;
        }

        .user-meta {
            font-size: 12px;
            color: #999;
            margin-top: 4px;
            display: flex;
            flex-wrap: wrap;
        }

        .user-meta span {
            margin-right: 15px;
        }

        .nickname {
            font-weight: bold;
            font-size: 14px;
        }

        /* 搜索区域样式优化 */
        .layui-inline {
            margin-bottom: 10px;
            margin-right: 10px;
        }

        /* 响应式布局 */
        @media screen and (max-width: 768px) {
            #pagination {
                flex-wrap: wrap;
            }

            .user-meta {
                flex-direction: column;
            }

            .user-meta span {
                margin-bottom: 3px;
            }
        }
    </style>
</head>

<body>
    <div class="layui-fluid layui-padding-3" style="background-color: #fff;">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="cate_name" lay-verify="required" placeholder="请输入" autocomplete="off"
                        class="layui-input" value="{$data.cate_name}">
                </div>
            </div>
            {if $data.pid != 0}
            <div class="layui-form-item">
                <label class="layui-form-label">父分类</label>
                <div class="layui-input-inline">
                    <select name="pid" class="layui-select" id="">
                        {foreach $cate as $k => $v}
                        <option value="{$v.id}" {$data.pid==$v.id ? 'selected' : '' }>{$v.cate_name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            {/if}
            <div class="layui-form-item">
                <label class="layui-form-label">是否显示</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switchTest" title="开启|关闭"
                        {$data.status==1 ? 'checked' : '' }>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否为私密板块(登录才能查看)</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_hide" lay-skin="switch" lay-filter="switchTest" title="开启|关闭"
                        {$data.is_hide==1 ? 'checked' : '' }>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">分类权重</label>
                <div class="layui-input-block">
                    <input type="number" name="sort" lay-verify="required|number" placeholder="请输入分类权重" autocomplete="off"
                        class="layui-input" value="{$data.sort}">
                </div>
            </div>

            {if $data.pid == 0}
            <div class="layui-form-item">
                <label class="layui-form-label">Icon</label>
                <div class="container">
                    <div class="image-box" data-index="0">
                        {if @$data.icon != ''}
                        <img src="{$data.icon}" alt="图片1">
                        <button class="delete-btn" style="display: inline-block;">×</button>
                        {else}
                        +
                        {/if}
                    </div>
                </div>
                <input type="file" id="fileInput" style="display: none;">

            </div>

            {/if}

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即修改</button>
                </div>
            </div>
        </form>
    </div>


    <script src="__LAYUI__/layui.js"></script>
    <script>
        layui.use(function () {
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;

            $ = layui.$;

            form.on('submit(demo1)', function (data) {
                var field = data.field;
                field.id = "{$data.id}";

                // 创建一个新的数据对象，不使用 data.field 自动收集的数据
                var submitData = {
                    id: "{$data.id}",
                    cate_name: field.cate_name,
                    pid: field.pid,
                    sort: field.sort,
                    status: field.status === 'on' ? 1 : 0,
                    is_hide: field.is_hide === 'on' ? 1 : 0,
                    icon: $('.image-box img').attr('src') || '',
                };

                $.post('/admin/category/doEdit', submitData, function (res) {
                    if (res.code === 200) {
                        layer.msg('修改成功', { icon: 1 });
                        setTimeout(function () {
                            location.reload();
                        }, 1500);
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                });
                return false;
            }); // 上传图片
            const uploadUrl = '/admin/category/uploadIcon';
            images = ''
            $('.image-box').click(function () {
                th = $(this)
                if ($(this).text().trim() === '+') {
                    $('#fileInput').click();
                    $('#fileInput').off('change').on('change', function (event) {
                        const file = event.target.files[0];
                        if (file) {
                            const formData = new FormData();
                            formData.append('icon', file);

                            $.ajax({
                                url: uploadUrl,
                                type: 'POST',
                                data: formData,
                                contentType: false,
                                processData: false,
                                success: function (res) {
                                    if (res.code == 200 && res.data) {
                                        th.html(`
                            <img src="${res.data}" alt="Uploaded Image">
                            <button class="delete-btn">&times;</button>
                        `);
                                        th.find('.delete-btn').show();
                                    } else {
                                        layer.msg(res.msg)
                                    }
                                },
                                error: function () {
                                    layer.msg('上传失败')
                                }
                            });
                        }
                    });
                }
            });

            $(document).on('click', '.delete-btn', function (e) {
                e.stopPropagation(); // 阻止事件冒泡到image-box
                $(this).parent().html('+');
            });
        });
    </script>
    </div>
</body>

</html>